<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
 * Liberty_reserver Class
 *
 * @author		Alexandre Tedeschi
 * @author		Phil Sturgeon
 * @version     1.0.0
 */
class Liberty_reserver {

	function __construct()
	{
		$this->ci =& get_instance();
		$this->ci->load->config('channel/channel');
	}
	
	/**
	 * This method is for transfering credit from system account to another account
	 * This method return an array of result processing information
	 * @param $lr_acc
	 * @param float $amount
	 */
	public function transfer($lr_payee_acc, $amount) {
		$lr_api_config = $this->ci->config->item('liberty_reserver_api');
		$lr_api_url = $lr_api_config['lr_api_url'];
		$lr_api_name = $lr_api_config['lr_api_name'];
		
		$user_id = $this->ci->current_user->id;
		
		$transaction_id = uniqid($user_id . '_');
		
		$authen_token = $this->build_authen_token($lr_payee_acc, $amount, $transaction_id);
		$result = $this->send_transfer_request($lr_payee_acc, $authen_token, $transaction_id, $amount);
		return $result;
		
	}
	
	private function send_transfer_request ($lr_payee_acc, $authen_token, $transaction_id, $amount) {
		$lr_api_config = $this->ci->config->item('liberty_reserver_api');
		$lr_api_url = $lr_api_config['lr_api_url'];
		$lr_api_name = $lr_api_config['lr_api_name'];
		$lr_api_user_agent = $lr_api_config['lr_api_user_agent'];
		$lr_api_payer_acc = $lr_api_config['lr_api_payer_acc'];
		$lr_api_merchant_ref = $lr_api_config['lr_api_merchant_ref'];
		$lr_api_currency = $lr_api_config['lr_api_currency'];
		$lr_api_memo = $lr_api_config['lr_api_memo'];
		$lr_api_purpose = $lr_api_config['lr_api_purpose'];
		$lr_api_private = $lr_api_config['lr_api_private'];
		
		$parameters = array();
		$parameters['id'] = $transaction_id;
		$parameters['account'] = $lr_api_payer_acc;
		$parameters['api'] = $lr_api_name;
		$parameters['token'] = $authen_token;
		$parameters['reference'] = $lr_api_merchant_ref;
		$parameters['type'] = 'transfer';
		$parameters['payee'] = $lr_payee_acc;
		$parameters['currency'] = $lr_api_currency;
		$parameters['amount'] = $amount;
		$parameters['memo'] = $lr_api_memo;
		$parameters['private'] = $lr_api_private;
		$parameters['purpose'] = $lr_api_purpose;
		
		$options = array(
	        CURLOPT_RETURNTRANSFER => true,     // return web page
	        CURLOPT_HEADER         => false,    // don't return headers
	        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
	        CURLOPT_ENCODING       => "",       // handle all encodings
	        CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x", // who am i
	        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
	        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
	        CURLOPT_TIMEOUT        => 120,      // timeout on response
	        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
	        CURLOPT_SSL_VERIFYHOST => 1,
	        CURLOPT_SSL_VERIFYPEER => false,
	        CURLOPT_SSL_VERIFYHOST => false,
	        CURLOPT_POSTFIELDS => $parameters
    	);
		
		$resource = curl_init($lr_api_url);
		curl_setopt_array($resource, $options);
		
		$response = curl_exec($resource);
		
		curl_close($resource);
		return $response;
	}
	
	/**
	 * build hash of these string:
	 * Security Word:ID:Reference:Payee:Currency:Amount:Date UTC in YYYYMMDD format:Time UTC in HH format (only hours, not minutes).
	 * Enter description here ...
	 */
	private function build_authen_token($lr_payee_acc, $amount, $transaction_id) {
		//https://api2.libertyreserve.com/nvp/transfer?id=333&account=U6090584&api=ltrlr1&token=245DF172750DD7673573369F165F06DE08886443F5FEA68722894558362DBD7C&reference=test&type=transfer&payee=U5594565&currency=usd&amount=1&memo=memo&private=false&purpose=service
		$lr_api_config = $this->ci->config->item('liberty_reserver_api');
		
		$lr_api_currency = $lr_api_config['lr_api_currency'];
		$lr_api_payer_acc = $lr_api_config['lr_api_payer_acc'];
		$lr_api_security_word = $lr_api_config['lr_api_security_word'];
		$lr_api_merchant_ref = $lr_api_config['lr_api_merchant_ref'];
		
		$utc_date = gmdate('Ymd:H');
		$authen_str = $lr_api_security_word . ':' . 
					  $transaction_id . ':' .
					  $lr_api_merchant_ref . ':' .
					  $lr_payee_acc . ':' .
					  $lr_api_currency . ':' .
					  $amount . ':' .
					  $utc_date;
		return hash('sha256', $authen_str);
	}
}

/* End of file LibertyReserver.php */